﻿/**
@author 迷途小羔羊
2012.11.18
*/
module GYLite
{				
	export class GYImage extends GYSprite
	{
		protected static _imgLoader:GYLoader;
		public static init():void
		{
			GYImage._imgLoader=new GYLoader(5);
		}
		protected _mySource:GYBitmap;
		protected _archorX:number;
		protected _archorY:number;
		protected _smoothing:boolean;				
		protected _texture:egret.Texture;
		public constructor()
		{
			super();
			var s = this;
			s._archorX = 0;
			s._archorY = 0;
			s._mySource = new GYBitmap;
			s.addEventListener(MouseEvent.ROLL_OVER, s.RlOver,s);
			s.addChild(s._mySource);
			s._mySource.setBatchDrawParam(s._batchDrawParam);
		}					
		public validBatch():void
		{
			let s = this;			
			s._mySource.validBatch();
		}				
		public set smoothing(val:boolean)
		{var s = this;
			s._smoothing = val;
			s._mySource.smoothing = val;
		}
		public get smoothing():boolean
		{var s = this;
			return s._mySource.smoothing;
		}
		public set source(bmp:egret.Texture)
		{var s = this;
			if(bmp == s._texture)
				return;
			s._texture = bmp			
			if(bmp)
			{
				if(s.$width == s.$width)
					s.scaleX=s.$width/bmp.textureWidth;
				if(s.$height == s.$height)
					s.scaleY=s.$height/bmp.textureHeight;
			}			
			s._mySource.smoothing = s._smoothing;				
			s._mySource.texture = s._texture;
			if(s.isBatch())
				s.validBatch();
			s.invalidDisplay();			
		}
		/**位图*/		
		public get source():egret.Texture
		{var s = this;
			return s._mySource.texture;
		}
		public set width(val:number)
		{var s = this;			
			if(s.$width == val)
				return;
			s.$width=val;
			if(s.$width != s.$width)
				s.scaleX=1;
			else
			{
				if(s._mySource.texture)
					s.scaleX=s.$width/s._mySource.texture.textureWidth;
			}
		}	
		public get width():number
		{var s = this;
			if(s.$width == s.$width)
				return s.$width;
			return egret.superGetter(GYImage, s, "width");
		}
		public set height(val:number)
		{var s = this;	
			if(s.$height == val)
				return;
			s.$height=val;
			if(s.$height != s.$height)
				s.scaleY=1;
			else
			{
				if(s._mySource.texture)
					s.scaleY=s.$height/s._mySource.texture.textureHeight;
			}
		}	
		public get height():number
		{var s = this;			
			if(s.$height == s.$height)
				return s.$height;
			return egret.superGetter(GYImage, s, "height");
		}
		protected RlOver(e:egret.TouchEvent):void
		{var s = this;
			if (GYSprite.isStageDown(this))
				s.dispatchEvent(GYSprite.mouseDownEvent);
		}

		public get archorX():number
		{var s = this;
			return s._archorX;
		}

		public set archorX(value:number)
		{var s = this;
			s._archorX = value;
			s._mySource.x = -s._archorX;
		}

		public get archorY():number
		{var s = this;
			return s._archorY;
		}

		public set archorY(value:number)
		{var s = this;
			s._archorY = value;
			s._mySource.y = -s._archorY;
		}

		public get bitmap():GYBitmap
		{let s = this;
			return s._mySource;
		}

		public setBatchDrawParam(val:BatchDrawParam)
		{
			let s= this;			
			super.setBatchDrawParam(val);			
			if(s._mySource)s._mySource.setBatchDrawParam(val);
		}
		/**合批图像的绘制样式**/
		public getBatchDrawParam():BatchDrawParam
		{
			return super.getBatchDrawParam();
		}
		public enableBatch(val:boolean)
		{var s = this;
			s._mySource.enableBatch(val);
			super.enableBatch(val);
		}
		/**是否动态合批，请在文本渲染前设定*/
		public isBatch():boolean
		{var s = this;
			return super.isBatch();
		}
	}
}